package co.edu.unal;

import java.util.Date;

import co.edu.unal.satacredito.rest.domain.general.CardType;
import co.edu.unal.satacredito.rest.domain.general.DocumentType;
import co.edu.unal.satacredito.rest.domain.general.GenericTransaction;
import co.edu.unal.satacredito.rest.domain.visa.CreditCard;
import co.edu.unal.satacredito.rest.domain.visa.CreditCardTransaction;
import co.edu.unal.satacredito.rest.domain.visa.ValidationStatus;
import co.edu.unal.satacredito.rest.representations.Representation;
import co.edu.unal.satacredito.rest.representations.general.GenericTransactionRepresentation;
import co.edu.unal.satacredito.rest.representations.visa.CreditCardTransactionRepresentation;
import co.edu.unal.satacredito.rest.util.RestSettings;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class RestTest {

	public static GenericTransaction createGenericTransaction() {
		return new GenericTransaction("pepe", "32234234324",
				DocumentType.CEDULA, "465465456654", "545", CardType.CREDIT,
				new Date(), "Banco", 1, new Date(), new Float(1200), "Compra");
	}

	public static void main(String[] args) {

		 testGenericService();

//		testVisaCreditCard();

	}

	/**
	 * 
	 */
	private static void testGenericService() {
		Client client = RestSettings.getInstance().getClient();

		WebResource webResource = client.resource(getURL()
				+ "RestWS/validation/generic/");

		ClientResponse clientResponse = webResource
				.type(Representation.SATACREDITO_MEDIA_TYPE)
				.accept(Representation.SATACREDITO_MEDIA_TYPE)
				.post(ClientResponse.class, createGenericTransaction());

		GenericTransactionRepresentation output = clientResponse
				.getEntity(GenericTransactionRepresentation.class);

		System.out.println("Output: "
				+ output.getGenericTransaction().getValidationResult());
	}

	private static void testVisaCreditCard() {
		Client client = RestSettings.getInstance().getClient();

		WebResource webResource = client.resource(getURL()
				+ "RestWS/validation/visa/creditCard/");

		ClientResponse clientResponse = webResource
				.type(Representation.SATACREDITO_MEDIA_TYPE)
				.accept(Representation.SATACREDITO_MEDIA_TYPE)
				.post(ClientResponse.class, createCreditCard());

		CreditCardTransactionRepresentation cardTransaction = clientResponse
				.getEntity(CreditCardTransactionRepresentation.class);

		System.out.println(cardTransaction.getTransaction().getValidationStatus());
	}

	private static CreditCardTransaction createCreditCard() {

		CreditCard creditCard = new CreditCard();

		creditCard.setDate(new Date());
		creditCard.setNameOnCard("PEPE");
		creditCard.setNumber("00000000000000");
		creditCard.setVerificationCode("456");

		CreditCardTransaction creditCardTransaction = new CreditCardTransaction();
		creditCardTransaction.setCard(creditCard);
		creditCardTransaction.setAmount(new Float(1200));
		creditCardTransaction.setDate(new Date());
		creditCardTransaction.setValidationStatus(ValidationStatus.UNKNOWN);

		return creditCardTransaction;
	}

	public static String getURL() {
		RestSettings.getInstance();
		return RestSettings.getREST_URL();
	}
}